<?php

/**
* Gavick News Image V - helper class
* @package Joomla!
* @Copyright (C) 2008 Gavick.com
* @ All rights reserved
* @ Joomla! is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.0 $
**/

// access restriction
defined('_JEXEC') or die('Restricted access');
// include com_content helper for article routing
require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');

class gkNewsImage5Helper
{
	
	var $module_id, 
	$group_id, 
	$show_text_block, 
	$text_block_opacity, 
	$text_block_background, 
	$text_block_bgcolor, 
	$clean_xhtml, 
	$readmore_text, 
	$readmore_button, 
	$title_link,  
	$animation_slide_speed, 
	$animation_interval, 
	$autoanimation, 
	$animation_slide_type, 
	$animation_text_type, 
	$useMoo, 
	$useScript, 
	$base_path_to_images, 
	$JSON, 
	$module_width, 
	$module_height, 
	$thumbnail_width, 
	$thumbnail_height, 
	$base_bgcolor, 
	$base_titlecolor, 
	$base_textcolor, 
	$base_linkcolor, 
	$base_hlinkcolor, 
	$slides, 
	$module_bg, 
	$cleanCode,
	$click_image,
	$JSONlinks;

	/**
		Initializing Class variables
	**/
	
	function initialize(&$params)
	{
		// error variable
		$error = false;
		// basic parameters of module
		$this->module_id = $params->get('module_id', 'news_image_5_1'); // module unique ID
		$this->group_id = $params->get('group_id', 0); 
		$this->module_bg = $params->get('module_bg', '');
		// text configuration
		$this->show_text_block = $params->get('show_text_block', 1);
		$this->text_block_opacity = $params->get('text_block_opacity', 0.45);
		$this->text_block_background = $params->get('text_block_background', 0);
		$this->text_block_bgcolor = $params->get('text_block_bgcolor', 0);
		$this->clean_xhtml = $params->get('clean_xhtml', 0);
		$this->readmore_text = $params->get('readmore_text','Read more');
		$this->readmore_button = $params->get('readmore_button',1);
		$this->title_link = $params->get('title_link',1);
		// animation configuration
		$this->animation_slide_speed = $params->get('animation_slide_speed', 0);
		$this->animation_interval = $params->get('animation_interval', 0);
		$this->autoanimation = $params->get('autoanimation', 0);
		$this->animation_slide_type = $params->get('animation_slide_type', 0);
		$this->animation_text_type = $params->get('animation_text_type', 0);
		// scripts configuration
		$this->clean_code = (bool) $params->get('clean_code', 1);
		$this->useMoo = $params->get('useMoo', 2);
		$this->useScript = $params->get('useScript', 2);
		// new option
		$this->click_image = (bool) $params->get('click_image', 1);
		// generating base path
		$uri =& JURI::getInstance();
		$this->base_path_to_images = $uri->root().'components/com_gk_photoslide/images';
		// first validation
        
        // Group ID validation - blank string
		if($this->group_id == '')
		{
			$error = 'You must specify Photoslide group to show in this module';	
		}
		// Group ID validation - not a number
		else if(preg_match('/$[0-9]+^/i',$this->group_id))
		{
			$error = 'Group ID isn\'t a number in your settings. You must set this value as integer.';	
		}
		
		// return error value to mainbody of module
        return $error;
	}

	/**
		Getting datas from database and prepare it...
	**/

	function getDatas()
	{
		// variable for error
		$error = false;
		
		$query = "
		SELECT 
			* 
		FROM 
			#__gk_photoslide_groups 
		WHERE 
			`id` = ".$this->group_id." 
		LIMIT 1;";
		
		$database = & JFactory::getDBO();
		$uri = JURI::getInstance();
		$database->setQuery($query);
		
		if( $dane = $database->loadObjectList() ){
			foreach($dane as $item){
				$this->module_width = $item->mediumThumbX;
				$this->module_height = $item->mediumThumbY;
		
				$this->base_bgcolor = $item->bgcolor;
				$this->base_titlecolor = $item->titlecolor;
				$this->base_textcolor = $item->textcolor;
				$this->base_linkcolor = $item->linkcolor;
				$this->base_hlinkcolor = $item->hlinkcolor;
			}
		}
		else
		{
			$error = 'In selected group is any slide to load. Add slides or change group in module settings...';
			return $error;
		}
		
		$query = "
		SELECT 
			`i`.`file` AS `name`, 
			`i`.`title` AS `title`, 
			`i`.`text` AS `text`, 
			`i`.`linktype` AS `linktype`, 
			`i`.`linkvalue` AS `linkvalue`, 
			`i`.`article` AS `article`, 
			`i`.`wordcount` AS `wordcount`, 
			`c`.`title` AS `ctitle`, 
			`c`.`sectionid` AS `sid`,
			`c`.`introtext` AS `introtext`, 
			`c`.`id` AS `cid` 
		FROM 
			#__gk_photoslide_slides AS `i` 
			LEFT JOIN 
				#__content AS `c` ON `i`.`article` = `c`.`id` 
		WHERE 
			`i`.`group_id` = ".$this->group_id." 
		ORDER BY 
			`i`.`order` ASC;";

		$database->setQuery($query);

		// prepare array
		$this->slides = array();
		$prepared_image = array();

		if($this->show_text_block == 1) $this->JSON = '<div class="gk_news_image_5_text_datas">';
		$this->JSONlinks;

		if( $datas = $database->loadObjectList() )
		{
			foreach($datas as $item)
			{
				unset($prepared_image);
		
				$prepared_image = array(
					'name' => $item->name,
					'title' => $item->title,
					'text' => $item->text,
					'linktype' => $item->linktype,
					'linkvalue' => $item->linkvalue,
					'article' => $item->article,
					'wordcount' => $item->wordcount,
					'introtext' => $item->introtext,
					'ctitle' => $item->ctitle,
					'sid' => $item->sid,
					'cid' => $item->cid
				);
		
				if($this->show_text_block == 1)
				{ 
					$slide_title = ($item->title == '') ? $item->ctitle: $item->title;
					$slide_text = ($item->text == '') ? $item->introtext: $item->text;
					$slide_textcolor = $this->base_textcolor;
		
					if($this->clean_xhtml == 1) $slide_text = strip_tags($slide_text);
		
					if($item->wordcount != 0)
					{
						$exploded_text = explode(" ",$slide_text);
						$slide_text = '';
						for($i = 0; $i < $item->wordcount;$i++) if(isset($exploded_text[$i])) $slide_text .= $exploded_text[$i].' ';
						$slide_text .= ' ';
					}
		
					 
					switch($item->linktype)
					{
						case 0:
							$slide_link = $item->linkvalue; 
						break;
						case 1: 
							$slide_link = JRoute::_(ContentHelperRoute::getArticleRoute($item->article, $item->cid, $item->sid));
						break;
						default: 
							$slide_link = '';
						break;
					}
		
					if($this->title_link == 0)
					{
						$slide_text = '<h2 style="color:'.$this->base_titlecolor.';">'.$slide_title.'</h2><p style="color:'.$slide_textcolor.';">'.$slide_text;
					}
					else
					{
						$hover_effect = '';
						$hover_effect .= 'style="color:'.$this->base_titlecolor.';"' ;
						$hover_effect .= ' onmouseover="this.style.color = \''.$this->base_hlinkcolor.'\';" ';
						$hover_effect .= 'onmouseout="this.style.color = \''.$this->base_titlecolor.'\'" ';
						$slide_text = '<h2><a href="'.$slide_link.'" '.$hover_effect.' class="gk_news_image_title">'.$slide_title.'</a></h2><p style="color:'.$slide_textcolor.';">'.$slide_text;
					}
		
					if($this->readmore_button == 1)
					{
						$slide_text .= ' <a href="'.$slide_link.'" class="gk_news_image_5_link readon">'.$this->readmore_text.'</a>';
					}
		
					$slide_text .= '</p>';
					$slide_text = str_replace("{mosimage}","",$slide_text);
			
					$this->JSON .= '<div class="gk_news_image_5_news_text">'.$slide_text.'</div><div class="gk_news_image_4_news_bgcolor">'.$this->base_bgcolor.'</div>';
				}
				
				switch($item->linktype)
				{
					case 0:
						$slide_link = $item->linkvalue; 
					break;
					case 1: 
						$slide_link = JRoute::_(ContentHelperRoute::getArticleRoute($item->article, $item->cid, $item->sid));
					break;
					default: 
						$slide_link = '';
					break;
				}
				
				$this->JSONlinks .= '<a href="'.$slide_link.'"></a>';
		
				array_push($this->slides, $prepared_image);
			}
		}
		else
		{
			$error = 'In selected group is any slide to load. Add slides or change group in module settings...';
			return $error;
		}
		
		if($this->show_text_block == 1) $this->JSON .= '</div>';
		
		return $error;
	}
	
	function generateContent()
	{
		// create instances of basic Joomla! classes
		$document =& JFactory::getDocument();
		$uri =& JURI::getInstance();
		
		// include file content.php and parse it
		require(JModuleHelper::getLayoutPath('mod_gk_news_image_5', 'content'));
		
		// add stylesheets to document header
		$document->addStyleSheet( $uri->root().'modules/mod_gk_news_image_5/css/style.css', 'text/css' );
		// init $headData variable
		$headData = false;
		
		// add scripts with automatic mode to document header
		if($this->useMoo == 2)
		{
			// getting module head section datas
			unset($headData);
			$headData = $document->getHeadData();
			// generate keys of script section
			$headData_keys = array_keys($headData["scripts"]);
			// set variable for false
			$mootools_founded = false;
			// searching phrase mootools in scripts paths
			for($i = 0;$i < count($headData_keys); $i++)
			{
				if(preg_match('/mootools/i', $headData_keys[$i]))
				{
					// if founded set variable to true and break loop
					$mootools_founded = true;
					break;
				}
			}
			// if mootools file doesn't exists in document head section
			if(!$mootools_founded)
			{
				// add new script tag connected with mootools from module
				$headData["scripts"][$uri->root().'modules/mod_gk_news_image_5/js/mootools.js'] = "text/javascript";
				// if added mootools from module then this operation have sense
				$document->setHeadData($headData);
			}
		}
		
		if($this->useScript == 2){
			// getting module head section datas
			unset($headData);
			$headData = $document->getHeadData();
			// generate keys of script section
			$headData_keys = array_keys($headData["scripts"]);
			// set variable for false
			$engine_founded = false;
			// searching phrase mootools in scripts paths
			if(array_search($uri->root().'modules/mod_gk_news_image_5/js/engine.js', $headData_keys) > 0)
			{
				// if founded set variable to true
				$engine_founded = true;
			}
			// if mootools file doesn't exists in document head section
			if(!$engine_founded)
			{
				// add new script tag connected with mootools from module
				$headData["scripts"][$uri->root().'modules/mod_gk_news_image_5/js/engine.js'] = "text/javascript";
				// if added mootools from module then this operation have sense
				$document->setHeadData($headData);
			}
		}
	
		// if clean code is enable use importer.php to include 
		// module settings in head section of document
		if($this->clean_code)
		{
			/* 
				add script tag with module configuration to document head section
			*/	
			
			// get head document section data 
			unset($headData);
			$headData = $document->getHeadData();
			// add new script tag to head document section data array	
			$headData["scripts"][$uri->root().'modules/mod_gk_news_image_5/js/importer.php?mid='.$this->module_id.'&amp;animation_slide_speed='.$this->animation_slide_speed.'&amp;animation_interval='.$this->animation_interval.'&amp;autoanimation='.$this->autoanimation.'&amp;animation_slide_type='.$this->animation_slide_type.'&amp;animation_text_type='.$this->animation_text_type.'&amp;base_bgcolor='.str_replace('#','',$this->base_bgcolor).'&amp;text_block_opacity='.$this->text_block_opacity] = "text/javascript";
			
			// if added mootools from module then this operation have sense
			$document->setHeadData($headData);
		} 
						
		// add default.php template to parse if it's needed
		if($this->useMoo != 2 || $this->useScript != 2 || !$this->clean_code)
		{
			require(JModuleHelper::getLayoutPath('mod_gk_news_image_5', 'default'));
		}
	}
	
	/**
		Showing informations about error - error string as attribute
	**/
	
	function error($error)
	{
		require(JModuleHelper::getLayoutPath('mod_gk_news_image_5', 'error'));
	}
}

?>